const app = getApp()
import * as echarts from '../../ec-canvas/echarts';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchList:[
      // {title:'男(1)',dataList:[{},{},{},{}]},
      // {title:'女(0)',dataList:[{},{},{},{}]}
    ],
    selectIndex:0,
    coursetInfo:{},
    sex:"1",//1男生 2女
    leftDataList:[],
    rightDataList:[],
    totalPage:0,
    pageNo:1,
    pageSize:120,
    isLoading:false,
    courseId:"",
    nanTotalCount:0,
    nvTotalCount:0,
    calssName:'',
    time:'',
    studentCount:0,
    classId:"",
    power_exponent:0, //极限警示

    //饼图数据
    lineEc:{
      lazyLoad: true,
    },
    timeValList:[],
    boyLine:[],
    girlLine:[],
    classLine:[],
  },


//折线图
drawLine:function(){
  let ecComponentdrawBar = this.selectComponent('#mychart-dom-line');
  ecComponentdrawBar.init((canvas, width, height, dpr) => {
    // 获取组件的 canvas、width、height 后的回调函数
    // 在这里初始化图表
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height,
      devicePixelRatio: dpr // new
    });
  
    canvas.setChart(chart);

    var option = {
      title: {
        text: '平均心率曲线',
        textStyle:{
          fontWeight: 'bolder',
          fontSize:'13',
          color: '#000',
  
        }
        
      },
      legend: {
        x:'right',
        y:'top',
        textStyle:{
          //fontWeight:bold,
          fontSize:'12',
          color: '#a9a8ad',
        },
        data: ['男生平均', '女生平均', '全班平均']
      },
      tooltip: {
        trigger: 'axis'
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      // toolbox: {
      //   feature: {
      //     saveAsImage: {}
      //   }
      // },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: this.data.timeValList,
        axisLabel: {
          fontSize:11,
          color: '#95969d', 
          rotate:'35',// 旋转度数
        },
        axisLine: {//y轴线的配置
          show: true,//是否展示
          lineStyle: {
              color: "#95969d",//y轴线的颜色（若只设置了y轴线的颜色，未设置y轴文字的颜色，则y轴文字会默认跟设置的y轴线颜色一致）
              width: 1,//y轴线的宽度
              type: "solid"//y轴线为实线
            },
          }


      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '男生平均',
          type: 'line',
         
          data: this.data.girlLine
        },
        {
          name: '全班平均',
          type: 'line',
         
          data: this.data.classLine
        },
        {
          name: '女生平均',
          type: 'line',
         
          data:this.data.girlLine
        }
      ]
      
    };
    chart.setOption(option);
    return chart;
  });
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      calssName:options.calssName,
      time:options.time,
      courseId:options.courseId,
      classId:options.classId,
    })
    console.log('接口数据当前用户角色33333:'+this.data.classId)
    this.getInfoData()
    
  },
  didClickIndex(e){
    let index = e.detail;
   this.setData({
     selectIndex:index
   })
   if(index == 0 && (this.data.leftDataList==null || this.data.leftDataList.length<=0)){
     this.setData({
      sex:"1",//1男生 2女
     })
     //this.getListData()
   }else if(index == 1 && (this.data.rightDataList==null || this.data.rightDataList.length<=0)){
    this.setData({
      sex:"2",//1男生 2女
     })
     this.getListData()
   }
    console.log('打印：'+index)
  },
  getInfoData(){
   
    var data = {
      courseId:this.data.courseId,
      classId:this.data.classId
    }
    app.api.getTeacherCoursetInfo(data).then(rest => {
     
      if(rest&&rest.success){   
        console.log('接口数据='+JSON.stringify(rest))
        this.setData({
          coursetInfo:rest.data,
          studentCount:rest.data.studentCount,
          time:rest.data.dayTimeValue+rest.data.startTimeValue+"-"+rest.data.endTimeValue,
          calssName:rest.data.className,
          power_exponent:parseInt((220-rest.data.grade)*0.9), //极限警示
        })
        this.getListData()
        this.setLineData(rest.data)
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },


  setLineData(data){
    var boyLine =[];
    var girlLine =[];
    var classLine =[];
    var timeValList =[];
    if(data.boyLine!=null){
      boyLine = data.boyLine.split(",")
    }
    if(data.girlLine!=null){
      girlLine = data.girlLine.split(",")
    }
    if(data.classLine!=null){
      classLine = data.classLine.split(",")
    }
    var endTime = parseInt(data.endTimeValue.substring(0,2))*60+parseInt(data.endTimeValue.substring(3,5));
    var startTime = parseInt(data.startTimeValue.substring(0,2))*60+parseInt(data.startTimeValue.substring(3,5));
    var time = (endTime-startTime)*60;
    console.log("-----1--------"+time);
    for(var i=0;(i*5)<time;i++){
        var val = (i*5);
        var fen = parseInt(val/60);
        var miao = val%60;
        if(fen > 0){
          val = fen+"'"+miao+"''"
        }else{
          val = miao+"''"
        }
        //console.log(val+"="+fen+"="+miao);
      timeValList.push(val)
    }
    //console.log("-----1--------"+timeValList.join(','));
    this.setData({
      
      boyLine:boyLine,
      girlLine:girlLine,
      classLine:classLine,
      timeValList:timeValList
    })

   this.drawLine()
  },
  
  getListData(cd){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading:true
    })
    var data = {
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
      courseId:this.data.courseId,
      xsxb:this.data.sex,//1男生 2女
    }
    console.log('+接口数据参数3='+JSON.stringify(data))
    app.api.getCoursetStuList(data).then(rest => {
      wx.hideLoading()
      this.setData({
        isLoading:false
      })
      cd && cd()
      if(rest&&rest.success){   
        console.log(this.data.selectIndex=='+接口数据3='+JSON.stringify(rest))
        if(this.data.selectIndex == 0){
          this.setData({
            leftDataList:[...this.data.leftDataList,...rest.data.list],
            totalPage:rest.data.totalCount,
            nanTotalCount:rest.data.totalCount
          })
          let temp1 ={
            title:"男("+ this.data.nanTotalCount+")",
            dataList:this.data.leftDataList
          }
          let temp2 ={
            title:"女("+ (this.data.studentCount - this.data.nanTotalCount)+")",
            dataList:this.data.rightDataList
          }
          this.setData({
            ['switchList[0]']:temp1,
            ['switchList[1]']:temp2,
          })

          this.setData({
            sex:"2",//1男生 2女
            selectIndex:1
           })
           this.getListData()
        }else{
          this.setData({
            rightDataList:[...this.data.rightDataList,...rest.data.list],
            totalPage:rest.data.totalCount,
            nanTotalCount:rest.data.totalCount
          })
        
          let temp2 ={
            title:"女("+this.data.nanTotalCount+")",
            dataList:this.data.rightDataList
          }
          this.setData({
            
            ['switchList[1]']:temp2,
          })
        }

        
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNo:1,
      dataList:[],
      totalPage:0
    })
    this.getListData(()=>{
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  
    if(this.data.pageSize * this.data.pageNo >= this.data.totalPage){
      return
    }

    if(this.data.isLoading){
      return
    }

    this.setData({
      pageNo:this.data.pageNo + 1
    })
    this.getListData()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})